package com.brutal.paint;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;

public class PaintTab {

	private boolean open = false;
	private boolean hovered = false;
	private final Image image;
	private Rectangle tab = null;
	private String text = null;
	private final int x;
	private final int y;

	public PaintTab(final Image image, final String text, final int x,
			final int y) {
		this.image = image;
		this.text = text;
		this.x = x;
		this.y = y;
	}

	private void drawStringBox(final Graphics2D g, String text, final int x,
			int y) {
		final Font font = new Font("Arial", 0, 9);
		Rectangle bounds = null;
		final int w = getStringBounds(g, text, x, y).width;
		final int h = getStringBounds(g, text, x, y).height;
		int i = 0;
		g.setColor(new Color(0, 0, 0, 100));
		g.fillRect(x, y, w, h);
		g.setColor(new Color(0, 0, 0));
		g.drawRect(x, y, w, h);
		text = "\n" + text;
		for (String s : text.split("\n")) {
			if (i == 0) {
				bounds = getStringBounds(g, s);
				drawTextWithShadow(g, s, font, x + bounds.x
						+ (w - bounds.width) / 2, y + bounds.y);
				y += text.split("\n").length;
				i++;
				continue;
			}
			bounds = getStringBounds(g, s);
			drawTextWithShadow(g, s, font, x + bounds.x + (w - bounds.width)
					/ 2, y + bounds.y);
			y += bounds.height + 4;
		}
	}

	private Rectangle getStringBounds(final Graphics2D g, String text,
			final int x, int y) {
		Rectangle bounds = null;
		int w = 0, h = 2, i = 0;
		text = "\n" + text;
		for (String s : text.split("\n")) {
			if (i == 0) {
				bounds = getStringBounds(g, s);
				y += text.split("\n").length;
				i++;
				continue;
			}
			bounds = getStringBounds(g, s);
			if (bounds.width > w) {
				w = bounds.width;
			}
			h += bounds.height;
			y += bounds.height;
		}
		return new Rectangle(x, y, w, h);
	}

	private Rectangle drawImageInStyledBox(final Graphics2D g,
			final Image image, final int x, final int y) {
		final int width = 26;
		final int height = 26;
		if (!open && !hovered) {
			g.setColor(new Color(25, 25, 25, 150));
			g.fillRect(x, y + height / 2, width, height / 2);
			g.setColor(new Color(255, 255, 255, 66));
			g.fillRect(x, y, width, height / 2);
		} else if (open || hovered) {
			g.setColor(new Color(255, 255, 255, 66));
			g.fillRect(x, y + height / 2, width, height / 2);
			g.setColor(new Color(25, 25, 25, 150));
			g.fillRect(x, y, width, height / 2);
		}
		g.setColor(new Color(0, 0, 0));
		g.drawRect(x, y, width, height);
		g.drawImage(image, x + 2, y + 2, null);
		return new Rectangle(x, y, width, height);
	}

	private void drawTextWithShadow(final Graphics2D g, final String s,
			final Font f, final int x, final int y) {
		final Color fg = Color.WHITE;
		final Color shadow = Color.BLACK;
		g.setFont(f);
		g.setColor(shadow);
		g.drawString(s, x + 1, y + 1);
		g.setColor(fg);
		g.drawString(s, x, y);
	}

	private Rectangle getStringBounds(final Graphics2D g, final String s) {
		final FontMetrics fm = g.getFontMetrics();
		final LineMetrics l = fm.getLineMetrics(s, g);
		final Rectangle2D r = fm.getStringBounds(s, g);
		return new Rectangle(0, (int) l.getAscent(), (int) r.getWidth(),
				(int) r.getHeight());
	}

	public void updateText(final String text) {
		this.text = text;
	}

	public boolean isOpen() {
		return open;
	}

	public boolean isHovered() {
		return hovered;
	}

	public boolean close() {
		open = false;
		hovered = false;
		return !open && !hovered;
	}

	public boolean open() {
		open = true;
		return open;
	}

	public boolean hover() {
		hovered = true;
		return hovered;
	}

	public Rectangle getBounds() {
		return tab;
	}

	public void onRepaint(final Graphics g1) {
		final Graphics2D g = (Graphics2D) g1;
		tab = drawImageInStyledBox(g, image, x, y);
		if (open || hovered) {
			final Rectangle r = getStringBounds(g, text, x, y);
			drawStringBox(g, text, x - r.width + tab.width, y - r.height);
		}
	}
}
